package com.lin.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.lin.utils.Session;
import com.yunfeng.supercall.activity.BaseActivity;

public class QRCodeBitmapView extends View {
	private Bitmap bitmap;
	
	float scalX = 0.5f;
	float scalY = 0.5f;
	
	public QRCodeBitmapView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Paint paint = new Paint();
		paint.setFilterBitmap(true);
		
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		int x = this.getWidth();
		int y = this.getHeight();
		double ratio;
		int temp = (y / x) - (h / w);
		if (temp > 0) {
			ratio = ((double) x) / w;
        } else {
        	ratio = ((double) y) / h;
        }
		w = (int) (w * ratio);
		h = (int) (h * ratio);
		
		Rect dest = new Rect(0, 0, w, h);
		canvas.save();
		canvas.translate((x - w) / 2, (y - h) / 2);
		String path = Session.getSession().getmUserBean().getPhoto();
		canvas.drawBitmap(bitmap, null, dest, paint);
		canvas.restore();
		
		/*canvas.save();
		Matrix matrix = new Matrix();
		int width = this.getWidth();//获取资源e资源位图的高
		float w = scalX / this.getWidth();
		float h = scalY / this.getHeight();
		matrix.postScale(w, h);//获取缩放比例
		Bitmap dstbmp = Bitmap.createBitmap(bitmap, 0, 0,
		                                    width, height, matrix, true); //根据缩放比例获取新的位图
		canvas.drawBitmap(dstbmp, 0, 0, paint); //在屏幕上画出位图
		canvas.restore();*/
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
        postInvalidate();
	}

	 public static Bitmap createBitmap(Context context) {
		 //String str = "http://superchat.api.yunfengapp.com:8989/" + BaseActivity.getUser(context).getUserid() + "";
		 String str = "http://app.chaoruan.net/?" + BaseActivity.getUser(context).getUserid() + "";
		 
	        try {
	            BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 300, 300);
	            int width = matrix.getWidth();
	            int height = matrix.getHeight();
	            int[] pixels = new int[width*height];
	            for(int y = 0; y<width; ++y){
	                for(int x = 0; x<height; ++x){
	                    if(matrix.get(x, y)){
	                        pixels[y*width+x] = 0xff000000; // black pixel
	                    } else {
	                        pixels[y*width+x] = 0xffffffff; // white pixel
	                    }
	                }
	            }
	            
	            Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	            bmp.setPixels(pixels, 0, width, 0, 0, width, height);
	            //bitmap = bmp;
	            //postInvalidate();
	            return bmp;
	        } catch (WriterException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

}